package com.example.demo.controller;

import com.example.demo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ConllectionManagerController")
@Api()
public class ConllectionFindController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation("添加数据")
    @PostMapping("addUser")
    public String addUser(
           @RequestBody User user
    ){
         User save = mongoTemplate.save(user);
//         mongoTemplate.insert(user);
//        mongoTemplate.insert(Arrays.asList(),User.class);
//        insert:插入重复数据时:insert 报 DuplicateKeyException提示主键重复;save对已存在的数据进行更新。
//        save:批处理操作时: insert可以一次性插入整个数据，效率较高;save需遍历整个数据，一次插入或更新，效率较低。
         return "成功";
    }

    @ApiOperation("查询数据")
    @GetMapping("findAllUser")
    public String findAllUser(){
         List<User> user = mongoTemplate.findAll(User.class);
         user.forEach(System.out::println);
         return "成功";
    }
    @ApiOperation("查询数据")
    @GetMapping("findUserById")
    public String findUserById(
            Integer id
    ){
        User user = mongoTemplate.findById(id, User.class);
        System.out.println(user);
        return "成功";
    }


    @ApiOperation("查询数据")
    @GetMapping("findUserByQuery")
    public String findUserByQuery(
            String userName
    ){
         Query query = Query.query(Criteria.where("name").is(userName));
         //and
         Query queryAnd = Query.query(Criteria.where("name").is(userName).and("id").is(0));
         //or
         Criteria criteria = new Criteria();
         criteria.orOperator(
                 Criteria.where("name").is(userName),
                 Criteria.where("name").is(userName)
         );

         //and or
        Criteria criteriaAndOr = new Criteria();
        criteriaAndOr.and("age").is(0)
                .orOperator(
                      Criteria.where("name").is(userName),
                      Criteria.where("name").is(userName)
               );
         mongoTemplate.find(Query.query(criteria), User.class);

         //sort
        Query querySort = new Query();
        querySort.with(Sort.by(Sort.Order.desc("salary")));
        List<User> users =  mongoTemplate.find(querySort,User.class);
        users.forEach(System.out::println);
        //limit
        Query queryLimit = new Query();
        queryLimit.with(Sort.by(Sort.Order.desc("salary"))).skip(0).limit(2);
        List<User> userLimit =  mongoTemplate.find(queryLimit,User.class);
        userLimit.forEach(System.out::println);
        //总条数
        mongoTemplate.count(new Query(),User.class);

        //去重
        mongoTemplate.findDistinct(new Query(),"salary",User.class,Double.class);

        return users.toString();

    }

    @ApiOperation("Json查询数据")
    @GetMapping("basicQuery")
    public void basicQuery(){
         BasicQuery basicQuery = new BasicQuery("{name:'string'}");
         List<User> users = mongoTemplate.find(basicQuery, User.class);
         users.forEach(System.out::println);
    }
}
